﻿initializeWindows = function(){

	// Tools
	MochaUI.builderWindow = function(){	
		new MochaUI.Window({
			id: 'builder',
			title: 'Window Builder',
			icon: 'images/icons/page.gif',
			loadMethod: 'xhr',
			contentURL: 'plugins/windowform/',
			onContentLoaded: function(){
				if ( !MochaUI.windowformScript == true ){
					new Request({
						url: 'plugins/windowform/scripts/Window-from-form.js',
						method: 'get',
						onSuccess: function() {
							$('newWindowSubmit').addEvent('click', function(e){
								new Event(e).stop();
								new MochaUI.WindowForm();
							});
							MochaUI.windowformScript = true;
						}.bind(this)
					}).send();
				}
			},
			width: 370,
			height: 410,
			maximizable: false,
			resizable: false,
			scrollbars: false
		});
	}
	if ($('builderLinkCheck')){
		$('builderLinkCheck').addEvent('click', function(e){
			new Event(e).stop();
			MochaUI.builderWindow();
		});
	}
	
	// Todo: Add menu check mark functionality for workspaces.
	
	// Workspaces
	
	if ($('saveWorkspaceLink')){
		$('saveWorkspaceLink').addEvent('click', function(e){
			new Event(e).stop();
			MochaUI.saveWorkspace();
		});
	}

	if ($('loadWorkspaceLink')){
		$('loadWorkspaceLink').addEvent('click', function(e){
			new Event(e).stop();
			MochaUI.loadWorkspace();
		});
	}

	// Help	
	MochaUI.featuresWindow = function(){
		new MochaUI.Window({
			id: 'features',
			title: 'Features',
			loadMethod: 'xhr',
			contentURL: 'pages/features-layout.html',
			width: 305,
			height: 175,
			resizeLimit: {'x': [275, 2500], 'y': [125, 2000]},
			toolbar: true,
			toolbarURL: 'pages/features-tabs.html'
		});
	}
	if ($('featuresLinkCheck')){
		$('featuresLinkCheck').addEvent('click', function(e){
			new Event(e).stop();
			MochaUI.featuresWindow();
		});
	}

	MochaUI.faqWindow = function(){
			new MochaUI.Window({
				id: 'faq',
				title: 'FAQ',
				loadMethod: 'xhr',
				contentURL: 'pages/faq.html',
				width: 750,
				height: 350
			});
	}
	if ($('faqLinkCheck')){
		$('faqLinkCheck').addEvent('click', function(e){
			new Event(e).stop();
			MochaUI.faqWindow();
		});
	}

	MochaUI.docsWindow = function(){
			new MochaUI.Window({
				id: 'docs',
				title: 'Documentation',
				loadMethod: 'xhr',
				contentURL: 'pages/docs.html',
				width: 750,
				height: 350,
				padding: [10,10,10,10,10]
			});
	}
	if ($('docsLinkCheck')){
		$('docsLinkCheck').addEvent('click', function(e){
			new Event(e).stop();
			MochaUI.docsWindow();
		});
	}

	MochaUI.resourcesWindow = function(){
			new MochaUI.Window({
				id: 'resources',
				title: 'Resources',
				loadMethod: 'xhr',
				contentURL: 'pages/resources.html',
				width: 300,
				height: 275,
				x: 20,
				y: 90 
			});
	}
	if ($('resourcesLinkCheck')){
		$('resourcesLinkCheck').addEvent('click', function(e){
			new Event(e).stop();
			MochaUI.resourcesWindow();
		});
	}

	MochaUI.helpWindow = function(){
			new MochaUI.Window({
				id: 'help',
				title: 'Support',
				loadMethod: 'xhr',
				contentURL: 'pages/support.html',
				width: 320,
				height: 320,
				x: 20,
				y: 90 
			});
	}
	if ($('helpLinkCheck')){
		$('helpLinkCheck').addEvent('click', function(e){
			new Event(e).stop();
			MochaUI.helpWindow();
		});
	}	

	MochaUI.contributeWindow = function(){
		new MochaUI.Window({
			id: 'contribute',
			title: 'Contribute',
			loadMethod: 'xhr',
			contentURL: 'pages/contribute.html',
			width: 320,
			height: 320,
			x: 20,
			y: 90 
		});
	}
	if ($('contributeLinkCheck')){
		$('contributeLinkCheck').addEvent('click', function(e){	
			new Event(e).stop();
			MochaUI.contributeWindow();
		});
	}

	MochaUI.aboutWindow = function(){
		new MochaUI.Window({
			id: 'about',
			title: 'MochaUI',
			loadMethod: 'xhr',
			contentURL: 'pages/about.html',
			type: 'modal2',
			width: 350,
			height: 195,
			contentBgColor: '#e5e5e5 url(images/logo2.gif) left 3px no-repeat',
			padding: { top: 43, right: 12, bottom: 10, left: 12 },
			scrollbars:  false
		});
	}
	if ($('aboutLink')){
		$('aboutLink').addEvent('click', function(e){
			new Event(e).stop();
			MochaUI.aboutWindow();
		});
	}

	// Deactivate menu header links
	$$('a.returnFalse').each(function(el){
		el.addEvent('click', function(e){
			new Event(e).stop();
		});
	});
	
	// Build windows onDomReady
	//MochaUI.parametricsWindow();
	
}

// Initialize MochaUI when the DOM is ready
window.addEvent('domready', function() {
    MochaUI.Desktop = new MochaUI.Desktop();
    MochaUI.Dock = new MochaUI.Dock();

    /* Create Columns
	 
	If you are not using panels then these columns are not required.
    If you do use panels, the main column is required. The side columns are optional.
    Create your columns from left to right. Then create your panels from top to bottom,
    left to right. New Panels are inserted at the bottom of their column.

	*/
    new MochaUI.Column({
        id: 'sideColumn1',
        placement: 'left',
        width: 220,
        resizeLimit: [100, 300]
    });

    new MochaUI.Column({
        id: 'mainColumn',
        placement: 'main',
        width: null,
        scrollbars: false,
        resizeLimit: [100, 300]
    });
    /*
    new MochaUI.Column({
    id: 'sideColumn2',
    placement: 'right',	
    width: 220,		
    resizeLimit: [195, 300]
    });
    */
    // Add panels to first side column
    // 项目类/管理类树形列表左菜单
    MochaUI.panelFileTree = function() {
        $('sideColumn1').empty();

        new MochaUI.Panel({
            id: 'left-panel',
            title: '项目类和管理类',
            loadMethod: 'iframe',
            contentURL: 'left-projects.aspx',
            column: 'sideColumn1',
            onContentLoaded: function() {
                if (!MochaUI.treeScript == true) {
                    new Request({
                        url: 'plugins/tree/scripts/tree.js',
                        method: 'get',
                        onSuccess: function() {
                            buildTree('tree1');
                            MochaUI.treeScript = true;
                        } .bind(this)
                    }).send();
                }
            },
            tabsURL: 'htmls/left-tabs.html'
        });
    }

    // 我的桌面
    MochaUI.panelMenu1 = function() {
        $('sideColumn1').empty();

        new MochaUI.Panel({
            id: 'left-desktop',
            title: '我的桌面',
            loadMethod: 'iframe',
            contentURL: 'left-menus.aspx?menu=menu1' + '&' + (new Date()).getTime(),
            column: 'sideColumn1'
        });
    }

    MochaUI.panelMenu1();   // 初始化显示 我的桌面

    // 管理维护
    MochaUI.panelMenu2 = function() {
        $('sideColumn1').empty();

        new MochaUI.Panel({
            id: 'left-twoTrees',
            title: '菜单导航',
            loadMethod: 'iframe',
            contentURL: 'left-menus.aspx?menu=menu2' + '&' + (new Date()).getTime(),
            column: 'sideColumn1'
        });
    }

    // 借阅管理
    MochaUI.panelMenu3 = function() {
        $('sideColumn1').empty();

        new MochaUI.Panel({
            id: 'left-twoTrees',
            title: '菜单导航',
            loadMethod: 'iframe',
            contentURL: 'left-menus.aspx?menu=menu3' + '&' + (new Date()).getTime(),
            column: 'sideColumn1'
        });
    }

    // 公共检索
    MochaUI.panelMenu4 = function() {
        $('sideColumn1').empty();

        new MochaUI.Panel({
            id: 'left-twoTrees',
            title: '菜单导航',
            loadMethod: 'iframe',
            contentURL: 'left-menus.aspx?menu=menu4' + '&' + (new Date()).getTime(),
            column: 'sideColumn1'
        });
    }

    // 系统管理
    MochaUI.panelMenu5 = function() {
        $('sideColumn1').empty();

        new MochaUI.Panel({
            id: 'left-twoTrees',
            title: '菜单导航',
            loadMethod: 'iframe',
            contentURL: 'left-menus.aspx?menu=menu5' + '&' + (new Date()).getTime(),
            column: 'sideColumn1'
        });
    }

    // 档案管理制度和流程
    MochaUI.panelMenu6 = function() {
        $('sideColumn1').empty();

        new MochaUI.Panel({
            id: 'left-twoTrees',
            title: '菜单导航',
            loadMethod: 'iframe',
            contentURL: 'left-menus.aspx?menu=menu6' + '&' + (new Date()).getTime(),
            column: 'sideColumn1'
        });
    }

    // 修改密码
    MochaUI.ChangePwd = function() {
        new MochaUI.Window({
            id: 'winChangePwd',
            title: '修改密码',
            loadMethod: 'iframe',
            contentURL: 'changepwd.aspx',
            width: 350,
            height: 200,
            resizable: false,
            maximizable: false,
            cornerRadius: 0
        });
    }
    /*
    new MochaUI.Panel({
    id: 'panel2',
    title: 'Panel',
    loadMethod: 'xhr',
    contentURL: 'pages/lipsum.html',
    column: 'sideColumn1',
    height: 0
    });

	new MochaUI.Panel({
    id: 'panel3',
    title: 'Panel',
    loadMethod: 'xhr',
    contentURL: 'pages/lipsum.html',
    column: 'sideColumn1',
    height: 140,
    footer: true,
    footerURL: 'pages/toolbox-demo.html'
    });
    */
    // Add panels to main column	
    new MochaUI.Panel({
        id: 'mainPanel',
        title: '默认首页',
        loadMethod: 'xhr',
        contentURL: 'pages/lipsum.html',
        column: 'mainColumn',
        panelBackground: '#fff'
    });
    /*
    new MochaUI.Panel({
    id: 'panel1',
    title: 'Panel',
    loadMethod: 'xhr',
    contentURL: 'pages/lipsum.html',
    column: 'mainColumn',
    height: 200
    });
    */
    // Add panels to second side column
    /*
    new MochaUI.Panel({
    id: 'help-panel',
    loadMethod: 'xhr',
    contentURL: 'pages/overview.html',
    column: 'sideColumn2',
    tabsURL: 'pages/panel-tabs.html'
    });

	new MochaUI.Panel({
    id: 'tips-panel',
    title: 'Tips',
    loadMethod: 'xhr',
    contentURL: 'pages/tips.html',
    column: 'sideColumn2',
    height: 140,
    footer: true,
    footerURL: 'pages/toolbox-demo.html'
    });
    */
    MochaUI.BorrowsRecord = function() {
        UpdateMainPanel('借阅/领用/打印申请记录', 'borrowsrecord.aspx');
    }

    MochaUI.BorrowsTodoList = function() {
        UpdateMainPanel('待处理借阅', 'borrowlist.aspx?status=0');
    }

    MochaUI.BorrowsOverList = function() {
        UpdateMainPanel('已处理借阅', 'borrowlist.aspx?status=1');
    }

    MochaUI.ArticleList = function() {
        UpdateMainPanel('档案管理制度和流程', 'articlelist.aspx');
    }

    MochaUI.GaoJiSearch = function() {
        UpdateMainPanel('高级查询', 'search.aspx');
    }

    MochaUI.Departmentals = function() {
        UpdateMainPanel('部门管理', 'departmentals.aspx');
    }

    MochaUI.NewMessage = function() {
        UpdateMainPanel('新消息', 'NewMessage.aspx');
    }

    MochaUI.Modal = new MochaUI.Modal();

    MochaUI.Desktop.desktop.setStyles({
        'background': '#fff',
        'visibility': 'visible'
    });
    initializeWindows();

});

// This runs when a person leaves your page.
window.addEvent('unload', function(){
	if (MochaUI) MochaUI.garbageCleanUp();
});

function UpdateMainPanel(title,url) {
    MochaUI.updateContent({
        'element': $('mainPanel'),
        'loadMethod': 'iframe',
        'url': url,
        'title': title,
        'scrollbars': false,
        'toolbarURL':'sss.aspx',
        'padding': { top: 0, right: 0, bottom: 0, left: 0 }
    });
    $('mainPanel').setStyle('background', '#fff');
}